d <- read.delim("Primary_Files/JournalArticles_ReplicationFiles.txt")
d <- d[!is.na(d$Year.Published),]
names(d)
names(d)[5:8] <- c("StateAv","RepRef","RepAv","UpReq")

d$StateAv
table(d$StateAv, d$RepAv)

#Proportion who state that replication files are available:
#"We found that 55% of publications employing statistical analysis stated that replication files were available at a website, though we were only able to find replication files for 38% of publications."
sum(d$StateAv=="Yes" & d$UpReq=="No")/length(d$StateAv)
#.55

sum(d$RepAv=="Yes")/length(d$StateAv)
#.38

#"4% of publications state that replication files are available upon request."
sum(d$UpReq=="Yes")/length(d$StateAv)
#0.04

#Proportion State Available by Journal
sum(d$StateAv=="Yes" & d$UpReq=="No" & d$Journal=="AJPS")/sum(d$Journal=="AJPS")
#.74
sum(d$StateAv=="Yes" & d$UpReq=="No" & d$Journal=="APSR")/sum(d$Journal=="APSR")
#.16

#Proportion Is Available by Journal
sum(d$RepAv=="Yes"  & d$Journal=="AJPS")/sum(d$Journal=="AJPS")
#.51
sum(d$RepAv=="Yes"  & d$Journal=="APSR")/sum(d$Journal=="APSR")
#.13



library("ggplot2")
library(colorspace)

d$y <- rep(NA, length(d[,1]))
d$y[d$StateAv=="Yes"] <- 1 
d$y[d$StateAv=="No"] <- 0

d$y2 <- rep(NA, length(d[,1]))
d$y2[d$RepAv=="Yes"] <- 1 
d$y2[d$RepAv=="No"] <- 0

d$x <- d$Year.Published
d$xj <- d$x + runif(length(d$x),-0.3,0.3)
d$yj <- d$y + runif(length(d$x),-0.03,0.03)
#d$yj <- 0.96*d$y + runif(length(d$x),-0.03,0.03)
d$y2j <- d$y2 + runif(length(d$x),-0.03,0.03)

height <- 6
#y <- y + rnorm(length(d[,1]),0,.1)
p1 <- ggplot(d, aes(y=y, x=as.numeric(Year.Published))) + facet_grid(. ~ Journal) +
ylab("Publication States that Replication Files Are Available") +  
  xlab("Year") 
#+ ylim(0,1)
p1 + stat_smooth(method="glm", family="binomial", formula= y ~ poly(x,3))  + 
  geom_point(aes(y=d$yj, x=d$xj)) +
#   theme(axis.ticks=element_blank(), panel.grid.minor.y=element_blank()) +
#   scale_y_continuous(breaks=c(0, 1), labels=c("No", "Yes"))
ggsave("Output/Fig1A.pdf", width=1.6*height, height=height)

p2 <- ggplot(d, aes(y=y2, x=as.numeric(Year.Published))) + facet_grid(. ~ Journal) +
  ylab("Replication Files Are Publicly Available") +  
  xlab("Year") 
#+ ylim(0,1)
p2 + stat_smooth(method="glm", family="binomial", formula= y ~ poly(x,3)) +
  geom_point(aes(y=d$y2j, x=d$xj))
ggsave("Output/Fig1B.pdf", width=1.6*height, height=height)


#c <- ggplot(d, aes(y=StateAv, x=Year.Published, colour=factor(Journal)))
#c + stat_smooth(method=lm, aes(fill = factor(Journal)))
